package com.daiqile.okgodemo;

import android.app.Application;

import com.blankj.utilcode.util.Utils;
import com.daiqile.okgodemo.base.Constants;
import com.daiqile.okgodemo.utils.EncryptUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.SPCookieStore;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpParams;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * @author Mr.Shi
 * @date Created on 2017/11/9.
 */

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        initOkGo();
        Utils.init(this);
    }



    private void initOkGo() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //配置log
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        //log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);
        //全局的连接超时时间
        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        //全局的写入超时时间
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        //全局的读取超时时间
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        //使用sp保持cookie，如果cookie不过期，则一直有效
        builder.cookieJar(new CookieJarImpl(new SPCookieStore(this)));
        //信任所有证书,不安全有风险
        HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
        builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
     /*   //设置头部
        HttpHeaders headers = new HttpHeaders();
        headers.put("xxx", "xxx");    //header不支持中文，不允许有特殊字符
        headers.put("xxx", "xxx");*/
        //设置参数
        final String time = EncryptUtils.getNowTimeStamp();
        final String public_key = EncryptUtils.createTimeStampToken(Long.valueOf(time), Constants.KEY_ONE);
        String first = "&time=" + time + "&public_key=" + public_key;
        final String sign = EncryptUtils.params2Asc2Sort(first, Constants.KEY_TWO);
        HttpParams params = new HttpParams();
        params.put("time", time);     //param支持中文,直接传,不要自己编码
        params.put("public_key", public_key);
        params.put("sign", sign);
        OkGo.getInstance().init(this)//初始化
                .setOkHttpClient(builder.build())//设置OkHttpClient
                .setCacheMode(CacheMode.NO_CACHE)//全局统一缓存模式(不使用缓存)
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)//全局统一缓存时间(永不过期)
                .setRetryCount(3)//全局统一超时重连次数
//                .addCommonHeaders(headers)//全局公共头
                .addCommonParams(params);//全局公共参数


    }
}
